package com.loklok.code.confuse.ui;

import com.intellij.ide.impl.ProjectUtil;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.loklok.code.confuse.model.CodePathConfig;
import com.loklok.code.confuse.util.CommonUtil;
import com.loklok.code.confuse.util.FilePackageChooseUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

/**
 * @Classname SettingConfigUI
 * @Description
 * @Date 2023/5/11 15:22
 * @Created by wangchangjiu
 */
public class SettingConfigUI {

    private JTextField controllerTextField;

    private JButton controllerButton;

    private JPanel settingPanel;
    private JTextField codeAuthorField;
    private JTextField requestText;
    private JButton requestBut;
    private JTextField voText;
    private JButton voBut;
    private JTextField deepSeekApiKeyText;
    private JTextField deepSeekPromptText;
    private JTabbedPane tabbedPane1;

    private JTextField codeTemplateText;
    private JButton codeTemplateBut;
    private JSpinner batchSpinner;
/*    private JTextArea voVmPText;
    private JTextArea dtoVmPText;
    private JTextArea controllerVmPText;*/
/*    private JTextField feignPckText;
    private JButton feignBut;*/
    private JTextField cueWordText;
    private JButton cueWordBut;
    private JTextArea notCollectClassText;

    private CodePathConfig codePathConfig = new CodePathConfig();

    public SettingConfigUI() {
        controllerButton.addActionListener(actionEvent -> FilePackageChooseUtils.choosePackageAndSetPackagePath(ProjectUtil.getActiveProject(), controllerTextField));
        requestBut.addActionListener(actionEvent -> FilePackageChooseUtils.choosePackageAndSetPackagePath(ProjectUtil.getActiveProject(), requestText));
        voBut.addActionListener(actionEvent -> FilePackageChooseUtils.choosePackageAndSetPackagePath(ProjectUtil.getActiveProject(), voText));
        codeTemplateBut.addActionListener(actionEvent -> FilePackageChooseUtils.chooseFolderAndSetPath(ProjectUtil.getActiveProject(), codeTemplateText));
        cueWordBut.addActionListener(actionEvent -> FilePackageChooseUtils.chooseFolderAndSetPath(ProjectUtil.getActiveProject(), cueWordText));
    }

    public void defaultSettingConfig() {
        this.deepSeekPromptText.setText(CommonUtil.DEFAULT_PROMPT);
        this.batchSpinner.setValue(5);

    }

    public void fillPath(CodePathConfig codePathConfig) {
        this.controllerTextField.setText(codePathConfig.getControllerPath());
        this.codeAuthorField.setText(codePathConfig.getCodeAuthor());
        this.requestText.setText(codePathConfig.getRequestPath());
        this.voText.setText(codePathConfig.getVoPath());
        this.deepSeekApiKeyText.setText(codePathConfig.getDeepSeekApiKey());
        this.deepSeekPromptText.setText(StringUtils.isEmpty(codePathConfig.getDeepSeekPrompt()) ? CommonUtil.DEFAULT_PROMPT : codePathConfig.getDeepSeekPrompt());
        this.codeTemplateText.setText(codePathConfig.getCodeTemplate());
        int batch = codePathConfig.getBatch() == null ? 5 : codePathConfig.getBatch();
        this.batchSpinner.setValue(batch);
        this.notCollectClassText.setText(codePathConfig.getNotCollectClass());
        this.cueWordText.setText(codePathConfig.getDeepSeekPromptFile());
        this.codePathConfig = codePathConfig;
    }

    public CodePathConfig getCodePathConfig() {
        this.codePathConfig.setCodeAuthor(this.codeAuthorField.getText());
        this.codePathConfig.setVoPath(this.voText.getText());
        this.codePathConfig.setControllerPath(this.controllerTextField.getText());
        this.codePathConfig.setRequestPath(this.requestText.getText());
        this.codePathConfig.setDeepSeekApiKey(this.deepSeekApiKeyText.getText());
        this.codePathConfig.setDeepSeekPrompt(this.deepSeekPromptText.getText());
        this.codePathConfig.setCodeTemplate(this.codeTemplateText.getText());
        if(this.batchSpinner.getValue() != null){
            this.codePathConfig.setBatch(((Number)this.batchSpinner.getValue()).intValue());
        } else {
            this.codePathConfig.setBatch(5);
        }
        this.codePathConfig.setNotCollectClass(this.notCollectClassText.getText());
        this.codePathConfig.setDeepSeekPromptFile(this.cueWordText.getText());
        return codePathConfig;
    }

    public JPanel getSettingPanel() {
        return settingPanel;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        settingPanel = new JPanel();
        settingPanel.setLayout(new FormLayout("fill:d:grow", "center:d:noGrow"));
        tabbedPane1 = new JTabbedPane();
        tabbedPane1.setTabLayoutPolicy(1);
        CellConstraints cc = new CellConstraints();
        settingPanel.add(tabbedPane1, cc.xy(1, 1, CellConstraints.DEFAULT, CellConstraints.TOP));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        tabbedPane1.addTab("基本配置", panel1);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(4, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(4, 3, new Insets(0, 0, 0, 0), -1, -1));
        panel2.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel3.setBorder(BorderFactory.createTitledBorder(null, "路径配置", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JLabel label1 = new JLabel();
        label1.setText("代码作者");
        panel3.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        codeAuthorField = new JTextField();
        panel3.add(codeAuthorField, new GridConstraints(0, 1, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setHorizontalTextPosition(11);
        label2.setText("Controller包路径");
        label2.setVerticalAlignment(0);
        label2.setVerticalTextPosition(0);
        panel3.add(label2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        controllerTextField = new JTextField();
        controllerTextField.setHorizontalAlignment(10);
        controllerTextField.putClientProperty("caretAspectRatio", new Double(0.0));
        controllerTextField.putClientProperty("caretWidth", new Integer(300));
        panel3.add(controllerTextField, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        controllerButton = new JButton();
        controllerButton.setHideActionText(false);
        controllerButton.setHorizontalAlignment(0);
        controllerButton.setText("选择");
        controllerButton.setVerticalAlignment(0);
        controllerButton.setVerticalTextPosition(0);
        panel3.add(controllerButton, new GridConstraints(1, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JLabel label3 = new JLabel();
        label3.setText("Request包路径");
        panel3.add(label3, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        requestText = new JTextField();
        panel3.add(requestText, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        requestBut = new JButton();
        requestBut.setText("选择");
        panel3.add(requestBut, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label4 = new JLabel();
        label4.setText("VO包路径");
        panel3.add(label4, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        voText = new JTextField();
        panel3.add(voText, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        voBut = new JButton();
        voBut.setText("选择");
        panel3.add(voBut, new GridConstraints(3, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new GridLayoutManager(4, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel2.add(panel4, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel4.setBorder(BorderFactory.createTitledBorder(null, "deep seek 配置", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JLabel label5 = new JLabel();
        label5.setText("api key");
        panel4.add(label5, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        deepSeekApiKeyText = new JTextField();
        panel4.add(deepSeekApiKeyText, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label6 = new JLabel();
        label6.setText("生成模板提示词");
        panel4.add(label6, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label7 = new JLabel();
        label7.setText("混淆提示词");
        panel4.add(label7, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        deepSeekPromptText = new JTextField();
        panel4.add(deepSeekPromptText, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label8 = new JLabel();
        label8.setText("过滤已使用词汇");
        label8.setToolTipText("开启过滤已使用词汇只能使用单线程生成代码");
        panel4.add(label8, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel5 = new JPanel();
        panel5.setLayout(new GridLayoutManager(3, 3, new Insets(0, 0, 0, 0), -1, -1));
        panel2.add(panel5, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel5.setBorder(BorderFactory.createTitledBorder(null, "本地代码模板路径配置", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JLabel label9 = new JLabel();
        label9.setText("代码模板路径");
        label9.setToolTipText("生成代码模板的路径，选择到template前一个目录即可");
        panel5.add(label9, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        codeTemplateText = new JTextField();
        panel5.add(codeTemplateText, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        codeTemplateBut = new JButton();
        codeTemplateBut.setText("选择");
        codeTemplateBut.setToolTipText("生成代码模板的路径，选择到template前一个目录即可");
        panel5.add(codeTemplateBut, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        /*final JLabel label10 = new JLabel();
        label10.setText("全量代码JSON配置路径");
        label10.setToolTipText("json 配置文件的路径，选择到具体的文件或者文件夹（选择文件夹会读取文件夹子下所有文件）");
        panel5.add(label10, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label11 = new JLabel();
        label11.setText("增量代码JSON配置路径");
        label11.setToolTipText("json 配置文件的路径，选择到具体的文件或者文件夹（选择文件夹会读取文件夹子下所有文件）");
        panel5.add(label11, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane();
        scrollPane1.setVerticalScrollBarPolicy(22);
        tabbedPane1.addTab("已使用的词汇", scrollPane1);*/
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return settingPanel;
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
        // 数值类型
        SpinnerModel numberModel = new SpinnerNumberModel(5, 1, 100, 1);
        batchSpinner = new JSpinner(numberModel);
    }
}
